<?php

//require the hmtl functions
require("HTMLFunctions.php");
require("ValidationFunctions.php");
require('ac_db.inc.php');

//set error reporting
error_reporting(E_ERROR | E_WARNING);

//set credentials for database
setcredentials("tr2286","databases8620", "w4111b.cs.columbia.edu/ADB");

//now create a DB instance
$db = new Db("Proj1", "Part3");

$email;
$errorText = "";

//see if any infomration was passed
if(count($_POST) == 0){
	
	//if not, redirect to login
	echo "<meta http-equiv='refresh' content='0;url=http://localhost/login.php'>";
	exit;
}
//otherwise see if only an email was passed
elseif(count($_POST) == 1){
	
	$email = $_POST['email'];
	
	//call method to display initial form 
	displayUploadPage($email, $errorText, $db);
}
else{
	
	$email = $_POST['email'];
	//call method to process upload
	processUpload($email, $errorText, $db);
}

//method to display the upload page
function displayUploadPage($email, $errorText, $db){
	
	//create the html beginning
	HTMLFunctions::printHTMLHead("Upload Funds");
	
	//create header bar
	HTMLFunctions::generateTitleBar("Upload Funds", $email, $db);
	
		//print the form hmtl
	echo "<div class=\"content\">";

	//print any potential error messages passed to the function
	echo "<p>$errorText</p>";
	
	#print actual form
	echo <<<END_HTML
	<table>
			<form action="uploadFunds.php" method=POST>
			<tr>
				<!-- Amount-->
				<td><p>Amount:*</p></td>
				<td><input type="text" name="amount"></td>
				
				<!-- now create a hidden field that resubmits the email so that the script knows what user is being passed back-->
				<input type="hidden" name="email" value="$email">
			</tr>
			<tr>
				<!--submit button-->
				<td><input type="submit" value="Submit"></form></td></tr><tr><td>
END_HTML;

	//create cancel button
	//create array for button
	$parameterArray = array();
	$parameterArray['email'] = $email;
		
	//call subroutine to display button that returns the user to the profile
	HTMLFunctions::generateButton("profile.php", "Cancel", $parameterArray);
	
	echo <<<END_HTML
	
	</td>
	</tr>
	</table>
END_HTML;
	
	//close the database connection
	$db->closeSession();
	
	//call function to create the end
	HTMLFunctions::printHTMLEnd();
}

function processUpload($email, $errorText, $db){
	
	//ensure that amount is number and greater than 0
	if(!is_numeric($_POST['amount'])){
		
		//show error and return to search page
		$errorText = "<h4><font color=\"red\">You did not enter a number.<br>Please try again.</font></h4>";
		displayUploadPage($email, $errorText, $db);
	}
	
	//that amount is positive
	if($_POST['amount'] <= 0){
		
		$errorText = "<h4><font color=\"red\">You entered 0 or a negative number.<br>Please try again.</font></h4>";
		displayUploadPage($email, $errorText, $db);
	}
	//otherwise proceed with search
	else{
		
		//display success and add to db
		$currentFunds = $db->getLiquidBalance($email);
		$currentFunds += $_POST['amount'];
		
		$db->setLiquidBalance($email, $currentFunds);
		
		//commit changes
		$db->commit();
		
		//now display success
		displaySuccess($email, $db);
	}
}

//function to show the result of the search to the user
function displaySuccess($email, $db){
	
	//create the html beginning
	HTMLFunctions::printHTMLHead("Funds Uploaded");
	
	//create header bar
	HTMLFunctions::generateTitleBar("You Uploaded Funds to Your Account", $email, $db);
	
	//now print the actual code that shows the result
	echo <<<END_HTML
	
	<tr><td>
	<p>Your uploaded {$_POST['amount']} dollars to the system.</p>
	<p>Click below to return to profile</p>
	
END_HTML;

	//create array for button
	$parameterArray = array();
	$parameterArray['email'] = $email;
	
	//call subroutine to display button that returns the user to the profile
	HTMLFunctions::generateButton("profile.php", "Go To Profile", $parameterArray);
	
	//close database connection
	$db->closeSession();
	
	//call function to create the end
	HTMLFunctions::printHTMLEnd();
}




